package git.bankview.wl.report;

import java.util.*;
import java.sql.*;
import java.io.*;

import laya.semi.process.*;

import git.bankview.sl.db.*;
import git.bankview.sl.report.ReportCommon;
import git.bankview.sl.report.SchdUtil;
import git.bankview.wl.bean.LogonBean;

import org.apache.log4j.Logger;

public class ReschdReportActivity
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(ReschdReportActivity.class);
	
	private static final String SQL_SRCENTRYFINDER =
		"select * from dqschdentry where schdid = ?";
	private static final String SQL_SRCPARAMFINDER =
		"select * from dqschdparam where schdid = ? order by paramindex";
	private static final String SQL_NEWENTRYUPDATER = 
		"update dqschdentry set transid = ?, schddate = ?, schdtype = ?, schdexport = ?, schdstatus = ? where schdid = ?";
	private static final String SQL_NEWPARAMUPDATER =
		"insert into dqschdparam (schdid, paramindex, paramname, paramvalue) values (?, ?, ?, ?)";
	/**
	 *
	 */
	public void execute(IProcessContext oCtx)
		throws Exception
	{
		String[] vParamName = oCtx.getActivityParameter().split(":");
		LogonBean oLogon = (LogonBean)oCtx.getField(vParamName[0]);
		int nSchdId = ((Number)oCtx.getField("schdid")).intValue();
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			PreparedStatement ps = oConn.prepareStatement(SQL_SRCENTRYFINDER);
			ps.setInt(1, nSchdId);
			ResultSet rs = ps.executeQuery();
			Vector entry = new Vector();
			if (0 == DBUtil.resultSetToList(entry, rs, 0, 0))
				throw new ActivityException(oCtx, "schdEntry.err.noSuchRecord");
			ps = oConn.prepareStatement(SQL_SRCPARAMFINDER);
			ps.setInt(1, nSchdId);
			rs = ps.executeQuery();
			Vector params = new Vector();
			DBUtil.resultSetToList(params, rs, 0, 0);
			
			Timestamp now = new Timestamp(new java.util.Date().getTime());
			nSchdId = SchdUtil.generateSchdId(oConn);
			oCtx.setField("schdid", new Integer(nSchdId));

			ps = oConn.prepareStatement(SQL_NEWENTRYUPDATER);
			Map entryRec = (Map)entry.get(0);
			ps.setString(1, (String)entryRec.get("transid"));
			ps.setTimestamp(2, now);
			ps.setInt(3, ((Number)entryRec.get("schdtype")).intValue());
			ps.setString(4, (String)entryRec.get("schdexport"));
			ps.setInt(5, ReportCommon.SCHD_STATUS_READYTORUN);
			ps.setInt(6, nSchdId);
			ps.executeUpdate();

			ps = oConn.prepareStatement(SQL_NEWPARAMUPDATER);
			for (int i = 0; i < params.size(); ++i)
			{
				Map paramRec = (Map)params.get(i);
				ps.setInt(1, nSchdId);
				ps.setInt(2, ((Number)paramRec.get("paramindex")).intValue());
				String paramName = (String)paramRec.get("paramname");
				ps.setString(3, paramName);
				if (ReportCommon.GRP_SCHD_USER.equals(paramName))
				{
					ps.setString(4, oLogon.getUsername());
				}
				else
				{
					ps.setString(4, (String)paramRec.get("paramvalue"));
				}
				ps.addBatch();
			}
			ps.executeBatch();
		}
		finally
		{
			oConn.close();
		}
		
	}
}
